/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "motor_control.h"
#include "bsp_uart.h"
#include <stdlib.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
Remote_t ALL_Remote;//整机控制状态


extern rc_info_t rc;
//0.001s
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
   if(htim == &htim2){
       ALL_Remote.time_count++;
   }
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM2_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	led_off();
	dbus_uart_init();

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    if(ALL_Remote.MODE_flag == STOP_MODE){//停止模式
      Motor_ALL_set(0);
      // ALL_Remote.Statu = Alone_Control_statu;
    }
    else if(ALL_Remote.MODE_flag == TILTED_MODE){//斜倾模式
      if( ALL_Remote.RC_R_statu == Joystick_D){//初始状态1归位
        FIRST_Origin_state_bend(&ALL_Remote);
      }
      else if( ALL_Remote.RC_R_statu == Joystick_U){//初始状态1归位
        SECOND_Origin_state_bend(&ALL_Remote);
      }
      else if(((ALL_Remote.RC_L_statu == Joystick_U && ALL_Remote.RC_R_statu == None) )){//开始前倾
        if(ALL_Remote.Statu == Initial_bend_statu_1){
          FrontBend_1(&ALL_Remote);
        }
        else if(ALL_Remote.Statu == Initial_bend_statu_2){
          FrontBend_2(&ALL_Remote);
        }
      }
      else if(((ALL_Remote.RC_L_statu == Joystick_D && ALL_Remote.RC_R_statu == None) )){//前倾回正
        if(ALL_Remote.Statu == Farward_statu_1){
          FrontBend_back_1(&ALL_Remote);
        }
        else if(ALL_Remote.Statu == Farward_statu_2){
          FrontBend_back_2(&ALL_Remote);
        }
      }
      else if((ALL_Remote.RC_L_statu == Joystick_L && ALL_Remote.RC_R_statu == None)){//向左
        if(ALL_Remote.Statu == Initial_bend_statu_1)//当前处于初始状态，左倾
          LeftBend_1(&ALL_Remote);
        else if(ALL_Remote.Statu == Initial_bend_statu_2)
          LeftBend_2(&ALL_Remote);
        else if(ALL_Remote.Statu == Right_statu_1)//当前处于右倾状态，右倾回正
          RightBend_back_1(&ALL_Remote);
        else if(ALL_Remote.Statu == Right_statu_2)
          RightBend_back_2(&ALL_Remote);
        
      }
      else if((ALL_Remote.RC_L_statu == Joystick_R && ALL_Remote.RC_R_statu == None)){//向右
        if(ALL_Remote.Statu == Initial_bend_statu_1)//当前处于初始状态，右倾
          RightBend_1(&ALL_Remote);
        else if(ALL_Remote.Statu == Initial_bend_statu_2)
          RightBend_2(&ALL_Remote);
        else if(ALL_Remote.Statu == Left_statu_1)//当前处于左倾状态，左倾回正
          LeftBend_back_1(&ALL_Remote);
        else if(ALL_Remote.Statu == Left_statu_2)
          LeftBend_back_2(&ALL_Remote);
      }
      else if(((ALL_Remote.RC_L_statu == Upper_left && ALL_Remote.RC_R_statu == None) || (ALL_Remote.RC_L_statu == None && ALL_Remote.RC_R_statu == Upper_left)) && ALL_Remote.Statu == Initial_bend_statu_1){
        complex_move_1(&ALL_Remote);
      }
      else if(((ALL_Remote.RC_L_statu == Down_right && ALL_Remote.RC_R_statu == None) || (ALL_Remote.RC_L_statu == None && ALL_Remote.RC_R_statu == Down_right)) && ALL_Remote.Statu == Complex_statu){
        complex_back_1(&ALL_Remote);
      }
    }
    else if(ALL_Remote.MODE_flag == VERTICAL_MODE){//垂直运动
      if(ALL_Remote.RC_L_statu == Joystick_D && ALL_Remote.RC_R_statu == Joystick_D){//初始状态2归位
        FIRST_Origin_state_lift(&ALL_Remote);
      }
      else if(((ALL_Remote.RC_L_statu == Joystick_U && ALL_Remote.RC_R_statu == None) || (ALL_Remote.RC_L_statu == None && ALL_Remote.RC_R_statu == Joystick_U)) && ALL_Remote.Statu == Initial_lift_statu_1){
        UP30_1(&ALL_Remote);
      }
      else if(((ALL_Remote.RC_L_statu == Joystick_D && ALL_Remote.RC_R_statu == None) || (ALL_Remote.RC_L_statu == None && ALL_Remote.RC_R_statu == Joystick_D)) && ALL_Remote.Statu == Up_statu){
        DOWN30_1(&ALL_Remote);
      }
    }
    else{//单独控制
      if(abs(rc.ch1) < 50 && abs(rc.ch2) < 50 && abs(rc.ch3) < 50 && abs(rc.ch4) < 50){//无控制
        Motor_ALL_set(0);
      }
      else if(rc.ch2 > 500 && rc.ch4 > 500){//全部伸长
        Motor_ALL_set(1);
        // ALL_Remote.Statu = Alone_Control_statu;
      }
      else if(rc.ch2 < -500 && rc.ch4 < -500){//全部收缩
        Motor_ALL_set(2);
        // ALL_Remote.Statu = Alone_Control_statu;
      }
      else{//单杆控制
        if(ALL_Remote.MODE_flag == ROD_1){//单杆1控制
          if((rc.ch2 > 500 && abs(rc.ch4) < 50) || (rc.ch4 > 500 && abs(rc.ch2) < 50)){//伸长
            Motor1_control(1);
          }
          else if ((rc.ch2 < -500 && abs(rc.ch4) < 50) || (rc.ch4 < -500 && abs(rc.ch2) < 50)){//收缩
            Motor1_control(2);
          }
          else{//停止
            Motor_ALL_set(0);
          }
        }
        else if(ALL_Remote.MODE_flag == ROD_2){//单杆2控制
          if((rc.ch2 > 500 && abs(rc.ch4) < 50) || (rc.ch4 > 500 && abs(rc.ch2) < 50)){//伸长
            Motor2_control(1);
          }
          else if ((rc.ch2 < -500 && abs(rc.ch4) < 50) || (rc.ch4 < -500 && abs(rc.ch2) < 50)){//收缩
            Motor2_control(2);
          }
          else{//停止
            Motor_ALL_set(0);
          }
        }
        else if(ALL_Remote.MODE_flag == ROD_3){//单杆3控制
          if((rc.ch2 > 500 && abs(rc.ch4) < 50) || (rc.ch4 > 500 && abs(rc.ch2) < 50)){//伸长
            Motor3_control(1);
          }
          else if ((rc.ch2 < -500 && abs(rc.ch4) < 50) || (rc.ch4 < -500 && abs(rc.ch2) < 50)){//收缩
            Motor3_control(2);
          }
          else{//停止
            Motor_ALL_set(0);
          }
        }
        else if(ALL_Remote.MODE_flag == ROD_4){//单杆4控制
          if((rc.ch2 > 500 && abs(rc.ch4) < 50) || (rc.ch4 > 500 && abs(rc.ch2) < 50)){//伸长
            Motor4_control(1);
          }
          else if ((rc.ch2 < -500 && abs(rc.ch4) < 50) || (rc.ch4 < -500 && abs(rc.ch2) < 50)){//收缩
            Motor4_control(2);
          }
          else{//停止
            Motor_ALL_set(0);
          }
        }
        else if(ALL_Remote.MODE_flag == ROD_5){//单杆5控制
          if((rc.ch2 > 500 && abs(rc.ch4) < 50) || (rc.ch4 > 500 && abs(rc.ch2) < 50)){//伸长
            Motor5_control(1);
          }
          else if ((rc.ch2 < -500 && abs(rc.ch4) < 50) || (rc.ch4 < -500 && abs(rc.ch2) < 50)){//收缩
            Motor5_control(2);
          }
          else{//停止
            Motor_ALL_set(0);
          }
        }
        else if(ALL_Remote.MODE_flag == ROD_6){//单杆6控制
          if((rc.ch2 > 500 && abs(rc.ch4) < 50) || (rc.ch4 > 500 && abs(rc.ch2) < 50)){//伸长
            Motor6_control(1);
          }
          else if ((rc.ch2 < -500 && abs(rc.ch4) < 50) || (rc.ch4 < -500 && abs(rc.ch2) < 50)){//收缩
            Motor6_control(2);
          }
          else{//停止
            Motor_ALL_set(0);
          }
        }
        else{//无控制，全部停止
          Motor_ALL_set(0);
        }
        // ALL_Remote.Statu = Alone_Control_statu;
      }
    }
    HAL_Delay(1);
  }
  /* USER CODE END 3 */

}
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 6;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1) 
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
